$(function () {
    //======================
    //======设置查询条件========
    //======================
    //内部公司
    $("#wlycwsfc_toolbar [name='gsId']").gsjgtree(1);
    //计划年份
    var yearSpinner = $("#wlycwsfc_toolbar [name='year']").numberspinner({
        min: 2000,
        max: 2100,
        editable: true
    });
    //计划月份
    var monthSpinner = $("#wlycwsfc_toolbar [name='month']").numberspinner({
        min: 1,
        max: 12,
        editable: true
    });


//======================
//========统计列表========
//======================
    var columns = [
        [
            {field: 'gsname', title: '园区', rowspan: 2, width: 100, align: 'center'},
            {field: 'qc', title: '期初', rowspan: 2, width: 100, align: 'right'},
            {title: '本期购入', colspan: 2, width: 100},
            {title: '本期发出', colspan: 1, width: 100},
            {field: 'qm', title: '期末', rowspan: 2, width: 100, align: 'right'}
        ],
        [
            {field: 'cgjs', title: '采购结算', width: 100, align: 'right'},
            {field: 'wljs', title: '物流结算', width: 100, align: 'right'},
            {field: 'xsjs', title: '销售结算', width: 100, align: 'right'}
        ]
    ];

    var grid = $('#wlycwsfc_grid').datagrid({
        url: $.webPath() + '/report/wlycwsfc/wlycwsfcList',
        toolbar: "#wlycwsfc_toolbar",
        rownumbers: true,
        singleSelect: true,
        collapsible: true,
        fit: true,
        pageNumber: 1,
        fitColumns: true,
        showFooter: true,
        queryParams: {
            year: $("#wlycwsfc_toolbar [name='year']").val(),
            month: $("#wlycwsfc_toolbar [name='month']").val()
        },
        onLoadSuccess: function () {
        },
        columns: columns
    });


//搜索按钮
    $("#wlycwsfc_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams: {
                year: $("#wlycwsfc_toolbar [name='year']").val(),
                month: $("#wlycwsfc_toolbar [name='month']").val(),
                gsId: $("#wlycwsfc_toolbar [name='gsId']").val()
            }});
    });

    $("#btn_wlycwsfc_cbjz").click(function () {
        var year = $("#wlycwsfc_toolbar [name='year']").val();
        var month =  $("#wlycwsfc_toolbar [name='month']").val();
        $.messager.confirm('提示', '是否确认对' + year + '年' + month + '月销售成本进行结转?', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/report/wlycwsfc/cbjz",
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        year: year,
                        month: month
                    },
                    beforeSend: function () {
                        $.mask();
                    },
                    success: function (data) {
                        $.mask.hide();
                        if (data.statusCode == 200) {
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            $("#cwsfc_toolbar [name='searchButton']").click();
                        } else {
                            $.messager.alert('提示', data.message, 'error');
                        }
                    },
                    error: function () {
                    }
                });
            }
        });
    });


});


